package com.yuzhitong.shapi.presenter;

import com.yuzhitong.shapi.base.BasePresenter;
import com.yuzhitong.shapi.base.Contents;
import com.yuzhitong.shapi.base.net.BaseListBean;
import com.yuzhitong.shapi.bean.MovieBean;
import com.yuzhitong.shapi.bean.SearchMovieBean;
import com.yuzhitong.shapi.contract.SearchCategoryContract;
import com.yuzhitong.shapi.model.SearchCategoryModel;
import com.yuzhitong.shapi.net.MyObserver;
import com.yuzhitong.shapi.net.RxScheduler;

import java.util.Map;

import io.reactivex.rxjava3.annotations.NonNull;

/**
 * Created by Brsya
 * CreateDate: 2022/5/10  20:11
 * Description:
 * 根据 name 条件搜索
 */
public class SearchCategoryPresenter extends BasePresenter<SearchCategoryContract.View> implements SearchCategoryContract.Presenter {
    private SearchCategoryContract.Model model;

    public SearchCategoryPresenter() {
        model = new SearchCategoryModel();
    }

    @Override
    public void getMovieList(int pageNumber, String name) {

        mView.showLoading();

        Map<String, Object> par = getPublicPar();
        if (name != null) {
            par.put("name", name);
        }
        par.put("page", pageNumber);
        par.put("size", Contents.PAGE_SIZE);

        model.getMovieList(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseListBean<MovieBean>>(mView) {
                    @Override
                    public void success(@NonNull BaseListBean<MovieBean> baseBean) {
                        baseBean.handleData(MovieBean.class);
                        if (baseBean.getData() != null && baseBean.getData().size() > 0) {
                            mView.showMovieList(baseBean.getData());
                        }
                        if (baseBean.isNoMore()) {
                            mView.showNoMore();
                        }
                    }
                });
    }
}
